
package UI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultCaret;

import model.CPU;

/**
 * OutputPanel displays the information of running processes, interrupt...
 * 
 * @author Sydney Lin
 * @version 1.0
 */
@SuppressWarnings("serial")
public class OutputPanel extends JPanel implements ActionListener, Observer
{
  /**
   * The panel width.
   */
  public static final int WIDTH = 600;

  /**
   * The panel height.
   */
  public static final int HEIGHT = 300;
  
  /**
   * Where to display.
   */
  private final JTextArea my_text_area;
  
  /**
   * The scroll pane.
   */
  private final JScrollPane my_scroll_pane;

  /**
   * Construct an OutputPanel. 
   */
  public OutputPanel()
  {
    super();
    my_text_area = new JTextArea();
    my_scroll_pane = new JScrollPane(my_text_area);

    my_text_area.setSize(WIDTH, HEIGHT);
    my_scroll_pane.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    my_text_area.setVisible(true);
    my_text_area.setAutoscrolls(true);
    my_text_area.setText("Report: \n\n");
    DefaultCaret caret = (DefaultCaret)my_text_area.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    add(my_scroll_pane);
    setVisible(true);

  }

  @Override
  public void actionPerformed(final ActionEvent arg0)
  {
    // TODO Auto-generated method stub

  }

  @Override
  public void update(final Observable the_object, final Object the_arg)
  {
    if (the_object instanceof CPU)
    {
      my_text_area.append((String) the_arg);
      my_text_area.append("\n");
      my_text_area.setText(my_text_area.getText());
    }
  }

  /**
   * Append a string of text in the output panel.
   * @param the_text The text.
   */
  public void appendText(final String the_text)
  {
    my_text_area.append(the_text);
  }
  
  /**
   * Clear the text area of this output panel.
   */
  public void clear()
  {
    my_text_area.setText("");
  }
}
